package mwn.droidbus.ui;


import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

import android.graphics.drawable.Drawable;
import android.util.Log;


/**
 * The Class Markers. Used as overlays on the Google Map to display busses, bus stops etc to the user. 
 * 
 * Pins Page: http://code.google.com/apis/chart/docs/gallery/dynamic_icons.html#pins
 * 
 * 
 */
public class Markers {

	/** The Constant chartsUrl. */
	private final static String chartsUrl = "http://chart.apis.google.com/chart";
		
	/** The cache. */
	private static HashMap<String, Drawable> cache = new HashMap<String, Drawable>();

	/**
	 * Creates the default marker. A canary yellow - idea bubble styled marker. 
	 *
	 * @return the marker
	 */
	public static Drawable createDefaultMarker() {
		Drawable marker = null;
		String key = "default";
		String parameters = "?chst=d_map_spin&chld=0.8|0|FFFF42|15|b|";

		if (cache.containsKey(key)) {
			marker = cache.get(key);
		} else {
			marker = createGoogleMarker(parameters, key);
		}
		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
		return marker;
	}

	/**
	 * Creates the bus marker, same as a default marker with a label e.g. the routeID on the pin marker
	 *
	 * @param id the id or routeID
	 * @param colour the colour
	 * @return the marker
	 */
	public static Drawable createBusMarker(String id, String colour) {
		Drawable marker = null;

		if (colour == null) {
			colour = "FFFF42"; //canary yellow
		}

		String parameters = "?chst=d_map_spin&chld=0.8|0|" + colour + "|15|b|" + id;

		if (cache.containsKey(id)) {
			marker = cache.get(id);
		} else {
			marker = createGoogleMarker(parameters, id);
		}
		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
		return marker;
	}

	/**
	 * Creates the bus stop marker. A white pin marker with a symbol of a bus on it. 
	 *
	 * @param id the id or stopID
	 * @return the marker
	 */
	public static Drawable createStopMarker(String id) {
		Drawable marker = null;
		String parameters = "?chst=d_map_pin_letter&chld=" + id + "|FFFFFF"; //"?chst=d_map_pin_icon&chld=bus|FFFFFF";

		if (cache.containsKey(id)) {
			marker = cache.get(id);
		} else {
			marker = createGoogleMarker(parameters, id);
		}
		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
		return marker;
	}

	/**
	 * Creates the bitmap marker using Google's Chart API
	 *
	 * @param chartsApiParameters the charts api parameters
	 * @param label the label for the pin
	 * @return the marker
	 */
	private static Drawable createGoogleMarker(String chartsApiParameters,String label) {
		Drawable marker = null;

		//TODO: Save pin marker image and draw/write text over it to save connecting to google
		try {
			Log.d("debug", "Request marker: " + label + " from Google.");
			//Log.d("debug", "url: " + chartsUrl + chartsApiParameters);
			URL completeUrl = new URL(chartsUrl + chartsApiParameters);
			HttpURLConnection conn = (HttpURLConnection) completeUrl.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream in = conn.getInputStream();
			marker = Drawable.createFromStream(in, "foo");
			cache.put(label, marker);
		} catch (Exception e) {
			Log.d("debug", "Something went wrong: " +e.toString());
		}
		return marker;
	}
}
